// pages/line/line.js
import * as echarts from '../../ec-canvas/echarts';

// 初始化图表函数  开始
let chart2 = null; 
function initChart2(canvas, width, height, dpr) {
  chart2 = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr
  })
  canvas.setChart(chart2)
  wx.getStorage({
    key:"data",
    success:function(res){
      var projectId = res.data[1]
      var projectSelectId = res.data[2]
      wx.request({
        url: 'http://localhost:8081/exam/getChapterList',
        data:{projectId:projectId,projectSelectId:projectSelectId},
        success:function(res){
          let chapterList = res.data
          console.log(chapterList);
          wx.request({
            url: 'http://localhost:8081/exam/getChapterRate',
            data:{projectId:projectId,projectSelectId:projectSelectId},
            success:function(res2){
              let data = res2.data
              console.log(data);
              let option = getOption(chapterList,data)
              chart2.setOption(option)
            }
          })
        }
      })
    }
  })
  return chart2
}

function getOption(chapterList,data){

  return {
    xAxis: {
      type: 'category',
      data: chapterList,
      axisLabel:{
        interval:0,
        rotate:45
      },
    },
    yAxis: {
      type: 'value',
      name:'百分比(%)',
      min:0,
      max:100
    },
    series: [
      {
        data: data,
        type: 'bar'
      }
    ]
  }
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    ec2: {
      onInit: initChart2
    },
    short:["多元函数积分学","无穷级数"]
  },  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    var _this = this
    wx.getStorage({
      key:"data",
      success:function(res){
        var projectId = res.data[1]
        var projectSelectId = res.data[2]
        wx.request({
          url: 'http://localhost:8081/exam/getShortList',
          data:{projectId:projectId,projectSelectId:projectSelectId},
          success:function(res1){
            _this.setData({
              short:res1.data
            })
          }
        })
      }
    })
  },

  navigator:function(){
    wx.navigateTo({
      url: '/pages/chapterSelect/chapterSelect',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})